package com.xinjian.jianba.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xinjian.jianba.dto.AgreeMeetDto;
import com.xinjian.jianba.dto.CancleMeetDto;
import com.xinjian.jianba.dto.InviteMeetDto;
import com.xinjian.jianba.dto.MeetAddressDto;
import com.xinjian.jianba.dto.MeetListDto;
import com.xinjian.jianba.dto.MeetListResultDto;
import com.xinjian.jianba.dto.RefuseMeetDto;
import com.xinjian.jianba.dto.RevokeMeetDto;
import com.xinjian.jianba.dto.ShopDetailVo;
import com.xinjian.jianba.dto.StartMeetDto;
import com.xinjian.jianba.dto.StartMeetResultDto;
import com.xinjian.jianba.dto.ShopListDto;
import com.xinjian.jianba.exception.Page;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.MeetService;
import com.xinjian.jianba.service.ShopListService;
import com.xinjian.jianba.service.ValidatorService;
import com.xinjian.jianba.utils.JsonUtil;
import com.xinjian.jianba.utils.LogBuilder;
import com.xinjian.jianba.utils.ModuleEnum;

@RestController
@RequestMapping("/api/v3/jianba/meet")
public class MeetController extends AbstractController {
	private static final Logger LOGGER = LoggerFactory.getLogger(MeetController.class);

	@Autowired
	private MeetService meetService;

	@Autowired
	private ValidatorService validatorService;
	
	@Autowired
	private ShopListService shopListService;

	/**
	 * 开始约见
	 *
	 * @author liuhaitao
	 * @param
	 * @return
	 * @exception
	 * @since JDK 1.8
	 */
//	@PostMapping(value = { "/start/meet" })
//	public String startMeet(@RequestBody StartMeetDto startMeetDto) {
//		LOGGER.info(LogBuilder.build(ModuleEnum.LOCATION, "开始约见,参数 startMeetDto={}"), JsonUtil.getJsonString(startMeetDto));
//		if (startMeetDto == null) {
//			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.PARAM_ERROR_MSG);
//		}
//
//		try {
//			validatorService.valid(startMeetDto);
//			Page<StartMeetResultDto> resultPage = meetService.startMeet(startMeetDto);
//			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, resultPage);
//		} catch (Exception e) {
//			LOGGER.error("------startmeet--error--->>>>>", e);
//			return this.responseClientJsonData(ResponseCode.ERROR, ResponseCode.ERROR_MSG);
//		}
//
//	}

	/**
	 * 邀请约见
	 *
	 * @author liuhaitao
	 * @param
	 * @return
	 * @exception
	 * @since JDK 1.8
	 */
	@PostMapping(value = { "/invite/meet" })
	public String inviteMeet(@RequestBody InviteMeetDto inviteMeetDto) {
		LOGGER.info(LogBuilder.build(ModuleEnum.LOCATION, "邀请约见,参数 inviteMeet={}"), JsonUtil.getJsonString(inviteMeetDto));
		if (inviteMeetDto == null) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.PARAM_ERROR_MSG);
		}

		try {
			validatorService.valid(inviteMeetDto);
			meetService.inviteMeet(inviteMeetDto);
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		} catch (Exception e) {
			LOGGER.error("------inviteMeet--error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.ERROR, e.getMessage());
		}

	}

	/**
	 * 同意约见
	 *
	 * @author liuhaitao
	 * @param
	 * @return
	 * @exception
	 * @since JDK 1.8
	 */
	@PostMapping(value = { "/agree/meet" })
	public String agreeMeet(@RequestBody AgreeMeetDto agreeMeetDto) {
		LOGGER.info(LogBuilder.build(ModuleEnum.LOCATION, "同意约见,参数 agreeMeet={}"), JsonUtil.getJsonString(agreeMeetDto));
		if (agreeMeetDto == null) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.PARAM_ERROR_MSG);
		}
		try {
			validatorService.valid(agreeMeetDto);
			meetService.agreeMeet(agreeMeetDto);
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		} catch (Exception e) {
			LOGGER.error("------agree--error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.ERROR, e.getMessage());
		}

	}

	/**
	 * 拒绝约见
	 *
	 * @param
	 * @return
	 * @exception
	 */
	@PostMapping(value = { "/refuse/meet" })
	public String refuseMeet(@RequestBody RefuseMeetDto refuseMeetDto) {
		LOGGER.info(LogBuilder.build(ModuleEnum.LOCATION, "拒绝约见,参数  refuseMeet={}"), JsonUtil.getJsonString(refuseMeetDto));
		if (refuseMeetDto == null) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.PARAM_ERROR_MSG);
		}
		try {
			validatorService.valid(refuseMeetDto);
			meetService.refuseMeet(refuseMeetDto);
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		} catch (Exception e) {
			LOGGER.error("------refuse--error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.ERROR, e.getMessage());
		}

	}

	/**
	 * 取消约见
	 *
	 * @author liuhaitao
	 * @param
	 * @return
	 * @exception
	 * @since JDK 1.8
	 */
	@PostMapping(value = { "/cancle/meet" })
	public String cancleMeet(@RequestBody CancleMeetDto cancleMeetDto) {
		LOGGER.info(LogBuilder.build(ModuleEnum.LOCATION, "取消约见,参数 cancleMeet={}"), JsonUtil.getJsonString(cancleMeetDto));
		if (cancleMeetDto == null) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.PARAM_ERROR_MSG);
		}
		try {
			validatorService.valid(cancleMeetDto);
			meetService.cancleMeet(cancleMeetDto);
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		} catch (Exception e) {
			LOGGER.error("------cancle--error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.ERROR, ResponseCode.ERROR_MSG);
		}

	}

	/**
	 * 撤销约见
	 *
	 * @author liuhaitao
	 * @param
	 * @return
	 * @exception
	 * @since JDK 1.8
	 */
	@PostMapping(value = { "/revoke/meet" })
	public String revokeMeet(@RequestBody RevokeMeetDto revokeMeetDto) {
		LOGGER.info(LogBuilder.build(ModuleEnum.LOCATION, "撤销约见,参数 revokeMeet={}"), JsonUtil.getJsonString(revokeMeetDto));
		if (revokeMeetDto == null) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.PARAM_ERROR_MSG);
		}
		try {
			validatorService.valid(revokeMeetDto);
			meetService.revokeMeet(revokeMeetDto);
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		} catch (Exception e) {
			LOGGER.error("------revoke--error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.ERROR, e.getMessage());
		}

	}

	/**
	 * 约见地址
	 *
	 * @author liuhaitao
	 * @param
	 * @return
	 * @exception
	 * @since JDK 1.8
	 */
	@PostMapping(value = { "/meet/address" })
	public String meetAddress() {
		try {
			List<MeetAddressDto> resultList = meetService.meetAddress();
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, resultList);
		} catch (Exception e) {
			LOGGER.error("------meetAddress--error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.ERROR, ResponseCode.ERROR_MSG);
		}

	}

	/**
	 * 我约列表
	 *
	 * @author liuhaitao
	 * @param
	 * @return
	 * @exception
	 * @since JDK 1.8
	 */
	@PostMapping(value = { "/meet/list" })
	public String meetList(@RequestBody MeetListDto meetListDto) {
		try {
			LOGGER.info(LogBuilder.build(ModuleEnum.LOCATION, "我约列表,参数  meetListDto={}"), JsonUtil.getJsonString(meetListDto));

			validatorService.valid(meetListDto);
			Page<MeetListResultDto> resultPage = meetService.meetList(meetListDto);
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, resultPage);
		} catch (Exception e) {
			LOGGER.error("------meetlist--error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.ERROR, ResponseCode.ERROR_MSG);
		}

	}

	/**
	 * 约我列表
	 *
	 * @author liuhaitao
	 * @param
	 * @return
	 * @exception
	 * @since JDK 1.8
	 */
	@PostMapping(value = { "/meet/listMe" })
	public String listMe(@RequestBody MeetListDto meetListDto) {
		try {
			LOGGER.info(LogBuilder.build(ModuleEnum.LOCATION, "约我列表,参数  meetListDto={}"), JsonUtil.getJsonString(meetListDto));

			validatorService.valid(meetListDto);
			Page<MeetListResultDto> resultPage = meetService.listMe(meetListDto);
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, resultPage);
		} catch (Exception e) {
			LOGGER.error("------listMe--error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.ERROR, ResponseCode.ERROR_MSG);
		}

	}
	/**
	 * 
	 * @param shopListDto
	 * @return
	 */
	@PostMapping("/shop/list")
	public String shopList(@RequestBody ShopListDto shopListDto){
		try {
			validatorService.valid(shopListDto);
		} catch (Exception e) {
			LOGGER.error("------shopList--error--->>>>>", e);
			return this.responseClientJsonData(ResponseCode.ERROR, ResponseCode.ERROR_MSG);
		}
		List<ShopDetailVo> resultList =shopListService.selectAll(shopListDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG, resultList);
	}
}
